﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using YArchitech.LIB;
using HYElectricKernel;
using HYElectricKernel.Generics;

namespace YArchitech.Revit.MEP.Calculator
{
	public partial class DlgBuildingFloors : System.Windows.Forms.Form
	{
		public DlgBuildingFloors(Autodesk.Revit.DB.Document doc, List<Level> lstLevels)
		{
			this.InitializeComponent();
			this.chklsFloors.CheckOnClick = true;
			this.InitalFloors(doc);
			foreach (Level level in this.m_lstAllLevels)
			{
				if (this.IsSelected(level, lstLevels))
				{
					this.chklsFloors.Items.Add(level.Name, true);
				}
				else
				{
					this.chklsFloors.Items.Add(level.Name, false);
				}
			}
			this.m_lstSelLevels = new List<Level>();
			base.Icon = Resource.YArchitechIcon;
		}

		private bool IsSelected(Level curLevel, List<Level> lstLevels)
		{
			if (lstLevels.Count <= 0)
			{
				return false;
			}
			using (List<Level>.Enumerator enumerator = lstLevels.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					if (enumerator.Current.Id.Equals(curLevel.Id))
					{
						return true;
					}
				}
			}
			return false;
		}

		public static List<Level> GetSortLevels(Autodesk.Revit.DB.Document doc, double bottom, double top)
		{
			List<Level> list = new List<Level>();
			foreach (Element element in new FilteredElementCollector(doc).OfClass(typeof(Level)).ToElements())
			{
				Level level = element as Level;
				double elevation = level.Elevation;
				if (Geometry.Greaterthan_Or_Equal(elevation, bottom) && Geometry.Lessthan_Or_Equal(elevation, top))
				{
					list.Add(level);
				}
			}
			list.Sort(new LevelComparer());
			return list;
		}

		private void InitalFloors(Autodesk.Revit.DB.Document doc)
		{
			this.m_lstAllLevels = DlgBuildingFloors.GetSortLevels(doc, double.MinValue, double.MaxValue);
		}

		private void btnAll_Click(object sender, EventArgs e)
		{
			for (int i = 0; i < this.chklsFloors.Items.Count; i++)
			{
				this.chklsFloors.SetItemChecked(i, true);
			}
		}

		private void btnNone_Click(object sender, EventArgs e)
		{
			for (int i = 0; i < this.chklsFloors.Items.Count; i++)
			{
				this.chklsFloors.SetItemChecked(i, false);
			}
		}

		private void btnReverse_Click(object sender, EventArgs e)
		{
			for (int i = 0; i < this.chklsFloors.Items.Count; i++)
			{
				if (this.chklsFloors.GetItemChecked(i))
				{
					this.chklsFloors.SetItemChecked(i, false);
				}
				else
				{
					this.chklsFloors.SetItemChecked(i, true);
				}
			}
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			for (int i = 0; i < this.chklsFloors.Items.Count; i++)
			{
				if (this.chklsFloors.GetItemChecked(i))
				{
					this.m_lstSelLevels.Add(this.m_lstAllLevels[i]);
				}
			}
			base.DialogResult = DialogResult.OK;
			base.Close();
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		public List<Level> m_lstSelLevels;

		private List<Level> m_lstAllLevels;
	}
}
